package com.lx.controller;

import com.lx.entity.Menu;
import com.lx.entity.MenuVO;
import com.lx.feign.MenuFeign;
import com.lx.feign.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author 李星
 * @date 2020/8/7 15:21
 */

@Controller
@RequestMapping("/menu")
public class MenuHandler {
    @Autowired
    private MenuFeign menuFeign;

    @Autowired
    private OrderFeign orderFeign;

    //分页查询全部菜品信息
    @GetMapping("/findAll")
    @ResponseBody
    public MenuVO findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        return menuFeign.findAll(page,limit);
    }

    //删除菜品
    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        orderFeign.deleteByMid(id);
        menuFeign.deleteById(id);
        return "redirect:/menu/redirect/menu_manage";
    }

    //修改菜品
    @GetMapping("/findById/{id}")
    public String findById(@PathVariable("id") long id, Model model){
        model.addAttribute("list",menuFeign.findAll());
        model.addAttribute("menu",menuFeign.findById(id));
        return "menu_update";
    }
    @PostMapping("/update")
    public String update(Menu menu){
//        System.out.println(menu+"-------");
        menuFeign.update(menu);
        return "redirect:/menu/redirect/menu_manage";
    }

    //添加菜品
    @GetMapping("/prepareSave")
    public String prepareSave(Model model){
        model.addAttribute("list",menuFeign.findAll());
        return "menu_add";
    }
    @PostMapping("/save")
    public String save(Menu menu){
        menuFeign.save(menu);
        return "redirect:/menu/redirect/menu_manage";
    }

    //访问页面
    @GetMapping("/redirect/{location}")
    public String index(@PathVariable("location") String location){
        return location;
    }

}
